// Copyright 2011 Google Inc. All Rights Reserved.
// Author: jacobsa@google.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var fi = flame.internal;
var buildFileGraph = fi.buildFileGraph;

//////////////////////////////////////////////////////
// Helpers
//////////////////////////////////////////////////////

function makeAction(inputs) {
  var action = new flame.Action(':some_action');
  action.inputs = inputs.concat([]);
  return action;
}

function sortedSubgraph(graph, node) {
  var reachableNodes = new fi.StringSet;
  graph.addReachableNodes(node, reachableNodes);

  var result = reachableNodes.getElements();
  result.sort();

  return result;
}

function BuildFileGraphTest() {
  this.map_ = new fi.StringMap;
}
registerTestSuite(BuildFileGraphTest);

//////////////////////////////////////////////////////
// Tests
//////////////////////////////////////////////////////

BuildFileGraphTest.prototype.NoOutputFiles = function() {
  // Shouldn't crash.
  var graph = buildFileGraph(this.map_);
};

BuildFileGraphTest.prototype.OneOutputFileWithNoInputs = function() {
  this.map_.insert('taco', makeAction([]));

  var graph = buildFileGraph(this.map_);

  expectThat(sortedSubgraph(graph, 'taco'), elementsAre(['taco']));
};

BuildFileGraphTest.prototype.OneOutputFileWithSomeInputs = function() {
  this.map_.insert('taco', makeAction(['burrito', 'enchilada']));

  var graph = buildFileGraph(this.map_);

  expectThat(sortedSubgraph(graph, 'taco'),
             elementsAre(['burrito', 'enchilada', 'taco']));

  expectThat(sortedSubgraph(graph, 'burrito'), elementsAre(['burrito']));
  expectThat(sortedSubgraph(graph, 'enchilada'), elementsAre(['enchilada']));
};

BuildFileGraphTest.prototype.MultipleOutputsNoInputs = function() {
  this.map_.insert('taco', makeAction([]));
  this.map_.insert('burrito', makeAction([]));
  this.map_.insert('enchilada', makeAction([]));

  var graph = buildFileGraph(this.map_);

  expectThat(sortedSubgraph(graph, 'taco'), elementsAre(['taco']));
  expectThat(sortedSubgraph(graph, 'burrito'), elementsAre(['burrito']));
  expectThat(sortedSubgraph(graph, 'enchilada'), elementsAre(['enchilada']));
};

BuildFileGraphTest.prototype.NonTrivialGraph = function() {
  this.map_.insert('taco', makeAction(['burrito', 'enchilada']));
  this.map_.insert('burrito', makeAction(['enchilada']));
  this.map_.insert('tortilla', makeAction(['enchilada']));
  this.map_.insert('enchilada', makeAction(['queso']));

  var graph = buildFileGraph(this.map_);

  expectThat(sortedSubgraph(graph, 'taco'),
             elementsAre(['burrito', 'enchilada', 'queso', 'taco']));

  expectThat(sortedSubgraph(graph, 'burrito'),
             elementsAre(['burrito', 'enchilada', 'queso']));

  expectThat(sortedSubgraph(graph, 'tortilla'),
             elementsAre(['enchilada', 'queso', 'tortilla']));

  expectThat(sortedSubgraph(graph, 'enchilada'),
             elementsAre(['enchilada', 'queso']));

  expectThat(sortedSubgraph(graph, 'queso'),
             elementsAre(['queso']));
};

BuildFileGraphTest.prototype.CycleInGraph = function() {
  this.map_.insert('taco', makeAction(['burrito', 'enchilada']));
  this.map_.insert('burrito', makeAction(['enchilada']));
  this.map_.insert('enchilada', makeAction(['burrito']));

  var map = this.map_;
  expectThat(function() { buildFileGraph(map) },
             throwsError(/Cycle.*graph.*(burrito|enchilada)/));
};
